#include "stdafx.h"
#include "wf_drawimage.h"

#include "pf_creation.h"
#include "pf_commands.h"
#include "pf_modification.h"

/**
 * Constructor.
 */
WF_DrawImage::WF_DrawImage(PF_Container& container,
                                       PF_GraphicView& graphicView)
        :PF_PreviewActionInterface("Image",
                           container, graphicView) {
}



WF_DrawImage::~WF_DrawImage() {}


QAction* WF_DrawImage::createGUIAction(LX_Define::ActionType /*type*/, QObject* /*parent*/) {
	// tr("Image")
	QAction* action = new QAction(tr("&Image"),  NULL);
	action->setIcon(QIcon(":/extui/menuimage.png"));
	//action->zetStatusTip(tr("Insert Image (Bitmap)"));
	return action;
}

void WF_DrawImage::init(int status) {
    PF_PreviewActionInterface::init(status);

    reset();

    data.file = PF_DIALOGFACTORY->requestImageOpenDialog();
	//   should we really redarw here?? graphicView->redraw();

	if (!data.file.isEmpty()) {
    	//std::cout << "file: " << data.file << "\n";

        img = QImage(data.file);
    	setStatus(SetTargetPoint);
	}
	else {
		finish();
		updateToolBar();
        //PF_DIALOGFACTORY->requestToolBar(LX_Define::ToolBarMain);
	}
}



void WF_DrawImage::reset() {
    data = PF_ImageAttribute(0,
                        PF_Vector(0.0,0.0),
                        PF_Vector(1.0,0.0),
                        PF_Vector(0.0,1.0),
                        PF_Vector(1.0,1.0),
                        "",
                        50, 50, 0);
}



void WF_DrawImage::trigger() {
    deletePreview();

    if (!data.file.isEmpty()) {
        PF_Creation creation(container, graphicView);
        creation.createImage(data);
    }

	graphicView->redraw(LX_Define::RedrawDrawing); 

    //PF_DIALOGFACTORY->requestToolBar(LX_Define::ToolBarMain);
    finish();
	updateToolBar();
}


void WF_DrawImage::mouseMoveEvent(PF_MouseEvent* e) {
    switch (getStatus()) {
    case SetTargetPoint:
        data.insertionPoint = snapPoint(e);

        deletePreview();
        //PF_Creation creation(preview, NULL, false);
        //creation.createInsert(data);
        PF_Line* line;
        line = new PF_Line(preview,
                           PF_LineAttribute(PF_Vector(0, 0),
                                       PF_Vector(img.width(), 0)));
        preview->addEntity(line);
        line = new PF_Line(preview,
                           PF_LineAttribute(PF_Vector(img.width(), 0),
                                       PF_Vector(img.width(), img.height())));
        preview->addEntity(line);
        line = new PF_Line(preview,
                           PF_LineAttribute(PF_Vector(img.width(),
                                                 img.height()), PF_Vector(0, img.height())));
        preview->addEntity(line);
        line = new PF_Line(preview,
                           PF_LineAttribute(PF_Vector(0, img.height()),
                                       PF_Vector(0, 0)));
        preview->addEntity(line);
        preview->scale(PF_Vector(0,0),
                       PF_Vector(data.uVector.magnitude(), data.uVector.magnitude()));
        preview->rotate(PF_Vector(0,0), data.uVector.angle());
        preview->move(data.insertionPoint);

        drawPreview();
        break;

    default:
        break;
    }
}



void WF_DrawImage::mouseReleaseEvent(PF_MouseEvent* e) {
    if (e->button()==Qt::LeftButton) {
        PF_CoordinateEvent ce(snapPoint(e));
        coordinateEvent(&ce);
    } else if (e->button()==Qt::RightButton) {
        //init(getStatus()-1);
		finish();
    }
}



void WF_DrawImage::coordinateEvent(PF_CoordinateEvent* e) {
    if (e==NULL) {
        return;
    }

    data.insertionPoint = e->getCoordinate();
    trigger();
}



void WF_DrawImage::commandEvent(PF_CommandEvent* e) {
    PF_String c = e->getCommand().lower();

    if (checkCommand("help", c)) {
        PF_DIALOGFACTORY->commandMessage(msgAvailableCommands()
                                         + getAvailableCommands().join(", "));
        return;
    }

    switch (getStatus()) {
    case SetTargetPoint:
        if (checkCommand("angle", c)) {
            deletePreview();
            lastStatus = (Status)getStatus();
            setStatus(SetAngle);
        } else if (checkCommand("factor", c)) {
            deletePreview();
            lastStatus = (Status)getStatus();
            setStatus(SetFactor);
        }
        break;

    case SetAngle: {
            bool ok;
            double a = PF_Math::eval(c, &ok);
            if (ok==true) {
                setAngle(PF_Math::deg2rad(a));
            } else {
                PF_DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
            }
            PF_DIALOGFACTORY->requestOptions(this, true, true);
            setStatus(lastStatus);
        }
        break;

    case SetFactor: {
            bool ok;
            double f = PF_Math::eval(c, &ok);
            if (ok==true) {
                setFactor(f);
            } else {
                PF_DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
            }
            PF_DIALOGFACTORY->requestOptions(this, true, true);
            setStatus(lastStatus);
        }
        break;

    default:
        break;
    }
}



PF_StringList WF_DrawImage::getAvailableCommands() {
    PF_StringList cmd;

    switch (getStatus()) {
    case SetTargetPoint:
        cmd += command("angle");
        cmd += command("factor");
        break;
    default:
        break;
    }

    return cmd;
}


void WF_DrawImage::showOptions() {
    PF_ActionInterface::showOptions();

    PF_DIALOGFACTORY->requestOptions(this, true);
}



void WF_DrawImage::hideOptions() {
    PF_ActionInterface::hideOptions();

    PF_DIALOGFACTORY->requestOptions(this, false);
}


void WF_DrawImage::updateMouseButtonHints() {
    switch (getStatus()) {
    case SetTargetPoint:
        PF_DIALOGFACTORY->updateMouseWidget(tr("Specify reference point"),
                                            tr("Cancel"));
        break;
    case SetAngle:
        PF_DIALOGFACTORY->updateMouseWidget(tr("Enter angle:"),
                                            "");
        break;
    case SetFactor:
        PF_DIALOGFACTORY->updateMouseWidget(tr("Enter factor:"),
                                            "");
        break;
    default:
        PF_DIALOGFACTORY->updateMouseWidget("", "");
        break;
    }
}



void WF_DrawImage::updateMouseCursor() {
    graphicView->setMouseCursor(LX_Define::CadCursor);
}



void WF_DrawImage::updateToolBar() {
    if (!isFinished()) {
        PF_DIALOGFACTORY->requestToolBar(LX_Define::ToolBarSnap);
    } else {
        PF_DIALOGFACTORY->requestToolBar(LX_Define::ToolBarMain);
    }
}


// EOF
